/*
 * code by mazheng at 2018-8-2 20:00
 * 
 * */

package com.ustcinfo.study.hbase.groupyangyang.mazheng;

import java.sql.*;

import org.apache.hadoop.hbase.client.Put;

import com.ustcinfo.ishare.bdp.hbase.HBaseMetaDao;
import com.ustcinfo.ishare.bdp.hbase.HBaseOperationDao;

public class MaZheng {
	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		//这里只是考虑实现，因此没有分层，没有考虑扩展性，没有捕获异常（直接抛出，不捕捉）SQL语句是直接作为字符串写到代码中
		//首先从mysql中取数据
    	//获取驱动类
    	Class.forName("com.mysql.jdbc.Driver");
		//创建连接
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testhbase","root","root");
		//创建一个sql语句，即陈述，陈述对象是conn
		Statement stmt = conn.createStatement();
		//陈述的语句类型是查询，因此使用executeQuery，
		ResultSet rs = stmt.executeQuery("select * from transfer_task_to");
		//循环遍历,将rs中的数据分别存储到两个list中，代表数据库的两个簇,将list中的属性全部转化为String类型的数据
		//List<String> list1 =new ArrayList<>();
		//List<String> list2 =new ArrayList<>();
		Put put = new Put("id".getBytes());
		//创建一个Hbase数据库,名称为“BD——TEST”，两个簇分别为F1，F2
		HBaseMetaDao hb = new HBaseMetaDao();
		hb.createTable("BD_TEST","F1","F2");
		
		while(rs.next()){
			//System.out.println(rs.getString("name")+" : "+rs.getInt("age"));
			
			//本来准备list插入，出现问题
			/*list1.add(String.valueOf(rs.getInt("id")));
			list1.add(String.valueOf(rs.getInt("task_id")));
			list1.add(rs.getString("create_time"));
			list1.add(rs.getString("start_time"));
			list1.add(rs.getString("out_filter_number"));
			list1.add(rs.getString("from_run_param"));
			list2.add(rs.getString("current_retry_times"));
			list2.add(rs.getString("retry_flag"));
			list2.add(String.valueOf(rs.getInt("parent_task_id")));
			list2.add(rs.getString("end_time"));
			list2.add(rs.getString("kill_time"));
			list2.add(rs.getString("failure_time"));*/
			//现在换成逐个录入
			put.addColumn("F1".getBytes(), "id".getBytes(), String.valueOf(rs.getInt("id")).getBytes());
			put.addColumn("F1".getBytes(), "task_id".getBytes(), String.valueOf(rs.getInt("task_id")).getBytes());
			put.addColumn("F1".getBytes(), "create_time".getBytes(), rs.getString("create_time").getBytes());
			put.addColumn("F1".getBytes(), "start_time".getBytes(), rs.getString("start_time").getBytes());
			put.addColumn("F1".getBytes(), "out_filter_number".getBytes(), rs.getString("out_filter_number").getBytes());
			put.addColumn("F1".getBytes(), "from_run_param".getBytes(), rs.getString("from_run_param").getBytes());
			put.addColumn("F2".getBytes(), "current_retry_times".getBytes(), rs.getString("current_retry_times").getBytes());
			put.addColumn("F2".getBytes(), "retry_flag".getBytes(), rs.getString("retry_flag").getBytes());
			put.addColumn("F2".getBytes(), "parent_task_id".getBytes(), String.valueOf(rs.getInt("parent_task_id")).getBytes());
			put.addColumn("F2".getBytes(), "end_time".getBytes(), rs.getString("end_time").getBytes());
			put.addColumn("F2".getBytes(), "kill_time".getBytes(), rs.getString("kill_time").getBytes());
			put.addColumn("F2".getBytes(), "failure_time".getBytes(), rs.getString("failure_time").getBytes());
			//提交
			HBaseOperationDao.put("BD_TEST", put);
		}
	
    	
    	
		//scanTable("test", null);
		// Put put = new Put("123".getBytes());
        //put.addColumn("family1".getBytes(), "age".getBytes(), "123".getBytes());
        //put.addColumn("family1".getBytes(), "name".getBytes(), "123".getBytes());
        //put("test4", put);
        //deleteAll("test4", "123");
        //delete("test4", "123", "family1", "name");
        //System.out.println(get("test4", "1"));
        //System.out.println(getCol("test4", "1", "family1"));
			
		//调用批量插入方法,目前还有一点问题，没有把String类型插入到Put类型中去
		//putList("BD_TEST", list1);
		//putList("BD_TEST", list2);
	
    	//不管打开了什么都关掉，后打开的先关掉，
		rs.close();
		stmt.close();
		conn.close();
    }
}